package com.weiyan.blog.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author misterWei
 * @create 2020年01月20号:18点01分
 * @mailbox mynameisweiyan@gmail.com
 */
@Configuration
//基于方法的认证权限
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true)
public class WebSecruityConfig extends WebSecurityConfigurerAdapter {

    //认证管理器配置
    @Bean
    public AuthenticationManager getAuthenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
       http.csrf().disable()
                .authorizeRequests()
/*
                  .antMatchers("/index").hasAnyAuthority("admin")//基于web的拦截管理
*/
                    .antMatchers("/**").authenticated() //所有请求必须通过认证
                           .antMatchers(HttpMethod.OPTIONS, "/oauth/**")
                 .permitAll()
               .and().cors().and().csrf().disable()
                            .formLogin()  //配置自定义的登陆业务
                              .loginPage("/auth/login_result").permitAll();
    }

    //密码加密处理器
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}
